﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="MarketsForm.cs" company="">
//   
// </copyright>
// <summary>
//   The markets form.
// </summary>
// --------------------------------------------------------------------------------------------------------------------


using System;
using System.Collections.Generic;
using System.Data.Objects;
using System.Data.Objects.DataClasses;
using System.Linq;
using System.Windows.Forms;
using Systemathics.FrameWork;

namespace Systemathics.UserControls
{
    public partial class MarketsForm : Form
    {
        private SystemathicsEntities db;
        private DataGridViewRow lastSelectedDataGridViewRow;

        public MarketsForm()
        {
            InitializeComponent();
        }

        private void MarketsForm_Load(object sender, EventArgs e)
        {
            db = new SystemathicsEntities(GlobalVariables.EntitiesConnectionString());
            db.AcceptAllChanges();
            dBExchangeBindingSource.DataSource = db.DBExchange.Execute(MergeOption.AppendOnly);
        }

        private void Update_Click(object sender, EventArgs e)
        {
            SetMarket(Grid);
        }

        private void SetMarket(PropertyGrid Grid)
        {
            var i = Grid.SelectedObject as OverridedDBExchange;
            var exchange = new DBExchange();
            if (i != null)
                if (i.Name != null)
                {
                    if (i.ID == null)
                    {
                        exchange.ID = Guid.NewGuid();
                        exchange.DBInstruments = new EntityCollection<DBInstruments>();
                    }
                    else
                    {
                        exchange = db.DBExchange.Where(p => p.ID.CompareTo((Guid) i.ID) == 0).FirstOrDefault();

                        // curr.DBInstruments.Load();
                        // foreach (var t in curr.DBInstruments.Take(curr.DBInstruments.Count).ToArray())
                        // curr.DBInstruments.Remove(t);
                    }

                    if (exchange.Exchange != String.Empty)
                    {
                        var sqlServerDateTime = new DateTime(1900, 1, 1);
                        exchange.Exchange = i.Exchange;
                        exchange.ExchangeName = i.Name;
                        exchange.OpenAuctions = i.OpenAuctions; //sqlServerDateTime + ;
                        exchange.Openning = i.Openning; //sqlServerDateTime + ;
                        exchange.CloseAuctions = i.CloseAuctions; //sqlServerDateTime + ;
                        exchange.Closing = i.Closing; //sqlServerDateTime + ;

                        try
                        {
                            db.AddToDBExchange(exchange);
                        }
                        catch
                        {
                        }

                        db.SaveChanges(true);
                    }
                }
        }

        private void AddItem_Click(object sender, EventArgs e)
        {
            Grid.SelectedObject = new OverridedDBExchange();
        }

        private void DeleteItem_Click(object sender, EventArgs e)
        {
            if (lastSelectedDataGridViewRow == null)
                return;

            int index = lastSelectedDataGridViewRow.Index;
            object o = lastSelectedDataGridViewRow.DataBoundItem;
            db.DeleteObject(o);
            db.SaveChanges(true);
            lastSelectedDataGridViewRow = ExchangeDataGridView.Rows[index];
        }

        private void ExcelItem_Click(object sender, EventArgs e)
        {
            ExchangeDataGridView.SelectAll();
            DataObject d = ExchangeDataGridView.GetClipboardContent();
            Clipboard.SetDataObject(d);
            MessageBox.Show("Exchanges Copied To ClipBorad", "Copy Orders", MessageBoxButtons.OK);
        }

        private void ExchangeDataGridView_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                lastSelectedDataGridViewRow = ((DataGridView) sender).Rows[e.RowIndex];
                var dbi = (DBExchange) lastSelectedDataGridViewRow.DataBoundItem;

                dbi.DBInstruments.Load();
                var temp = new List<string>();
                foreach (DBInstruments t in dbi.DBInstruments)
                    temp.Add(t.Name);

                Grid.SelectedObject = new OverridedDBExchange
                                          {
                                              ID = dbi.ID, 
                                              CloseAuctions = dbi.CloseAuctions, 
                                              Closing = dbi.Closing, 
                                              Country = dbi.Country, 
                                              Exchange = dbi.Exchange, 
                                              Name = dbi.ExchangeName, 
                                              OpenAuctions = dbi.OpenAuctions, 
                                              Openning = dbi.Openning, 
                                              InstrumentsCollection = temp
                                          };
            }
            catch (Exception)
            {
            }
        }

        protected override void OnHandleDestroyed(EventArgs e)
        {
            db.Dispose();
            base.OnHandleDestroyed(e);
        }
    }
}